import 'package:flutter/material.dart';
import '../model/CommonModel.dart';
import '../common/Constants.dart';
import 'dart:convert';

// 单读问
class AskPage extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return AskState();
  }

}

class AskState extends State<AskPage> {
  List<dynamic> list = [];

  @override
  void initState() {
    super.initState();
    loadData();
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(5),
      child:  GridView.builder(
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 1,
            mainAxisSpacing: 5,
            childAspectRatio: 2.0
          ),
          itemCount: list.length,
          itemBuilder: (context, index) {
            return Card(
              child: Stack(
                children: <Widget>[
                  ConstrainedBox(
                    constraints: BoxConstraints.expand(),
                    child: FadeInImage.assetNetwork(
                        placeholder: Constants.LOCAL_BIG_IMAGE,
                        image: (list[index] as Datas).thumbnail,
                      fit: BoxFit.cover,)
                  ),
                  Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      Padding(
                          padding: EdgeInsets.symmetric(horizontal: 8, vertical: 5),
                          child: Text(
                            (list[index] as Datas).title,
                            textAlign: TextAlign.center,
                            style: TextStyle(
                                color: Colors.white,
                                fontSize: 30
                            ),
                          )
                      ),
                      Text(
                        (list[index] as Datas).updateTime,
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: 12
                        ),
                      )
                    ],
                  )
                ],
              )
            );
          }
      )
    );
  }

  void loadData() async {
    String str = await DefaultAssetBundle.of(context).loadString("json/DanduwenData.json");
    CommonModel model = CommonModel.fromJson(json.decode(str));
    setState(() {
      list = model.datas;
      print("size：${list.length}");
    });
  }
}